﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace JasLib.Interop.Win32
{
    public static partial class OLEAUT32
    {
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayAccessData", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayAccessData([In] LPSAFEARRAY psa, [Out] out UINT_PTR ppvData);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayAllocData", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayAllocData([In] LPSAFEARRAY psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayAllocDescriptor", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayAllocDescriptor([In] uint cDims, [Out] out LPSAFEARRAY ppsaOut);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayAllocDescriptorEx", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayAllocDescriptorEx([In] VARTYPE vt, [In] uint cDims, [Out] out LPSAFEARRAY ppsaOut);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayCopy", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayCopy([In] LPSAFEARRAY psa, [Out] out LPSAFEARRAY ppsaOut);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayCopyData", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayCopyData([In] LPSAFEARRAY psaSource, [In] LPSAFEARRAY psaTarget);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayCreate", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern LPSAFEARRAY SafeArrayCreate([In] VARTYPE vt, [In] uint cDims, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] params SAFEARRAYBOUND[] rgsabound);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayCreateEx", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern LPSAFEARRAY SafeArrayCreateEx([In] VARTYPE vt, [In] uint cDims, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] SAFEARRAYBOUND[] rgsabound, [In] UINT_PTR pvExtra);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayCreateVector", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern LPSAFEARRAY SafeArrayCreateVector([In] VARTYPE vt, [In] int lLbound, [In] uint cElements);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayCreateVectorEx", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern LPSAFEARRAY SafeArrayCreateVectorEx([In] VARTYPE vt, [In] int lLbound, [In] uint cElements, [In] UINT_PTR pvExtra);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayDestroy", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayDestroy([In] UINT_PTR psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayDestroyData", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayDestroyData([In] LPSAFEARRAY psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayDestroyDescriptor", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayDestroyDescriptor([In] LPSAFEARRAY psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetDim", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetDim([In] LPSAFEARRAY psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetElement", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetElement([In] LPSAFEARRAY psa, [In, MarshalAs(UnmanagedType.LPArray)] int[] rgIndices, [In] UINT_PTR pv);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetElemsize", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetElemsize([In] LPSAFEARRAY psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetIID", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetIID([In] LPSAFEARRAY psa, [Out] out Guid pguid);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetLBound", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetLBound([In] LPSAFEARRAY psa, [In] uint nDim, [Out] out int plLbound);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetRecordInfo", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetRecordInfo([In] LPSAFEARRAY psa, [Out] out IRecordInfo prinfo);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetUBound", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetUBound([In] LPSAFEARRAY psa, [In] uint nDim, [Out] out int plUbound);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayGetVartype", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayGetVartype([In] LPSAFEARRAY psa, [Out] out VARTYPE pvt);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayLock", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayLock([In] LPSAFEARRAY psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayPtrOfIndex", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayPtrOfIndex([In] LPSAFEARRAY psa, [In] ref int rgIndices, [Out] out UINT_PTR ppvData);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayPutElement", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayPutElement([In] LPSAFEARRAY psa, [In] ref int rgIndices, [In] UINT_PTR pv);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayRedim", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayRedim([In] LPSAFEARRAY psa, [In] ref SAFEARRAYBOUND psaboundNew);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArraySetIID", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArraySetIID([In] LPSAFEARRAY psa, [In, MarshalAs(UnmanagedType.LPStruct)] Guid guid);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArraySetRecordInfo", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArraySetRecordInfo([In] LPSAFEARRAY psa, [In] IRecordInfo prinfo);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayUnaccessData", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayUnaccessData([In] LPSAFEARRAY psa);
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SafeArrayUnlock", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
        public static extern HRESULT SafeArrayUnlock([In] LPSAFEARRAY psa);

        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SysAllocString", CharSet = CharSet.Unicode)]
        public static extern UINT_PTR SysAllocString(string sz);

        /// <summary>
        /// Also implemented in .NET as System.Runtime.InteropServices.Marshal.FreeBSTR().
        /// </summary>
        [DllImport(Constants.ModuleNames.OLEAUT32, EntryPoint = "SysFreeString", CharSet = CharSet.Unicode)]
        public static extern void SysFreeString(UINT_PTR bstr);
    }
}
